﻿/***********************************************************************
 * <copyright file="SqlServerRoleDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Sunday, March 23, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// SqlServerRole class
    /// </summary>
    public class SqlServerRoleDao : IRole
    {
        /// <summary>
        /// Gets the role.
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <returns></returns>
        public Role GetRole(int roleId)
        {
            const string sql = @"uspRole_GetByID";
            object[] parms = { "@RoleID", roleId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the role.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <returns></returns>
        public Role GetRole(string roleName)
        {
            const string sql = @"uspRole_GetByName";
            object[] parms = { "@RoleName", roleName };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the roles.
        /// </summary>
        /// <returns></returns>       
        public List<Role> GetRoles()
        {
            const string sql = @"uspRole_GetAll";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        /// <summary>
        /// Gets the roles by active.
        /// </summary>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns></returns>
        public List<Role> GetRolesByIsActive(bool isActive)
        {
            const string sql = @"uspRole_GetByIsActive";
            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalPage">The total page.</param>
        /// <returns></returns>
        public List<Role> GetPaged(int currentPage, ref int totalPage)
        {
            const string sql = @"uspRole_GetPaging";
            object[] parms = { "@CurrentPage", currentPage, "@TotalPage", totalPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make,out totalPage, parms);
        }

        /// <summary>
        /// Gets the roles for user.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public List<Role> GetRolesForUser(int userId)
        {
            const string sql = @"uspRole_GetRolesForUser";
            object[] parms = { "@UserId", userId};
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Inserts the role.
        /// </summary>
        /// <param name="userGroup">The user group.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object InsertRole(Role userGroup)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Updates the role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>        
        public object UpdateRole(Role role)
        {
            const string sql = @"uspRole_Update";
            return Db.Update(sql, CommandType.StoredProcedure, TakeUpdate(role));
        }

        /// <summary>
        /// Deletes the role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object DeleteRole(Role role)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Role> Make = reader =>
          new Role
          {
              RoleId = reader["RoleId"].AsId(),
              Title = reader["Title"].AsString(),
              RoleName = reader["RoleName"].AsString(),
              Description = reader["Description"].AsString(),
              IsActive = reader["IsActive"].AsBool()
          };

        /// <summary>
        /// Creates query parameters list from UserGroup object
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns>
        /// Name value parameter list.
        /// </returns>
        private static object[] TakeUpdate(Role role)
        {
            return new object[]  
            {
                "@RoleId", role.RoleId,
                "@Title", role.Title,
                "@RoleName", role.RoleName,
                "@Description", role.Description,
                "@IsActive", role.IsActive
            };
        }
    }
}
